package levelEditor;

import breakout2014.LevelFactoryLoader;
import breakout2014.SpatialFactory;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LevelEditorFactory {
    private static final Logger logger = Logger.getLogger(LevelEditor.class.getName());
    private SpatialFactory spatialFactory;
    private LevelFactoryLoader levelFactoryLoader;
    private SimpleApplication context;
    private Node guiNode;
    private Spatial playground;
    
    public LevelEditorFactory(SimpleApplication context) {
            Logger.getLogger(LevelEditor.class.getName()).setLevel(Level.INFO); // SEVERE  INFO
            this.context = context;
            guiNode  = context.getGuiNode();
            spatialFactory = new SpatialFactory (context);
            levelFactoryLoader = new LevelFactoryLoader(context);
    }
    
    public void bildLevelEditor() {
        guiNode.detachAllChildren();
        
        // ######## BACKGROUND
        Spatial background = spatialFactory.buildSpatial("background", "EditorTextures/v2a.jpg");
        background.setLocalTranslation(context.getContext().getSettings().getWidth()/2, context.getContext().getSettings().getHeight()/2, 0);
        float factorBackY = context.getContext().getSettings().getHeight() / ((BoundingBox)background.getWorldBound()).getYExtent();
        float factorBackX = context.getContext().getSettings().getWidth() / ((BoundingBox)background.getWorldBound()).getXExtent();
        background.scale(factorBackX/2,factorBackY/2,0);
        guiNode.attachChild(background);

        // ######## PLAYGROUND BACKGROUND
        playground = spatialFactory.buildSpatial("background", "EditorTextures/playgroundBlack.png");
        playground.setLocalTranslation(770, context.getContext().getSettings().getHeight()/2, 0);
        playground.scale(1024,768,0);
        guiNode.attachChild(playground);
        
        // crate bricks sub tree
        Node brickBlueprints = new Node("brickBlueprints");
        guiNode.attachChild(brickBlueprints);

        // create brick
        Spatial brick = spatialFactory.buildSpatial("brickSimple");
        Vector3f pos = new Vector3f(100,400,0);
        brick.setLocalTranslation(pos);
        brickBlueprints.attachChild(brick);
    }
    
    public boolean buildLevel(String fileName) {
        if (fileName.matches("")){
            levelFactoryLoader.buildEmptyLevel(guiNode);
            return true;
        }
        else{
            return levelFactoryLoader.buildLevel(fileName, guiNode);
        }
    }
    
    public BoundingBox getPlaygroundBoundingBox() {
        return (BoundingBox)playground.getWorldBound();
    }
}
